p <- diamonds %>% sample_n(1000) %>% ggplot(aes(x = x, y = price)) + geom_point() + labs(x = "Diamond width (x-dim)", y = "Price") p
plotlyplotly tries to guess the right plot type
diamonds %>% sample_n(1000) %>% plot_ly(x = ~x, y = ~price)
plotlyBetter to specify with add_[plottype]
diamonds %>% sample_n(1000) %>% plot_ly(x = ~x, y = ~price) %>% add_markers()
plotlyStyling options use plotly syntax
diamonds %>% sample_n(1000) %>%
plot_ly(x = ~x, y = ~price) %>%
add_markers(color = I("red")) %>%
layout(xaxis = list(title = "Diamond width (x-dim)"),
yaxis = list(title = "Price"))
plotly“Aesthetic” options also use plotly syntax
diamonds %>% sample_n(1000) %>%
plot_ly(x = ~x, y = ~price, color = ~clarity, colors = "Set2") %>%
add_markers() %>%
layout(xaxis = list(title = "Diamond width (x-dim)"),
yaxis = list(title = "Price"))
Open 01-ia-exercise.R.
Make the plot below interactive using plotly. Reproduce as much of the plot as you can (colours, titles, etc)
ggplotlyStep 1: Make the ggplot
# with colour by clarity p <- diamonds %>% sample_n(1000) %>% ggplot(aes(x = x, y = price, colour = clarity)) + geom_point() + geom_smooth(se = FALSE) + labs(x = "Diamond width (x-dim)", y = "Price") + scale_color_brewer(palette = "Set2") p
ggplotlyStep 1: Make the ggplot
ggplotlyStep 2: ggplotly the ggplot!
ggplotly(p)
Open 02-ia-exercise.R.
Differences between clarity groups in the previous plot might be easier to see if we plot densities rather than all points. Replot the previous plot as a 2-D density plot, coloured by clarity, using the geom_density2d geom.
# one of the plots we made in the previous lecture
occupancies <- st_read("../spatial-data/data/Mongolia_SL.shp",
quiet = TRUE)
sights <- read.csv("../spatial-data/data/confirmed-sightings.csv")
sights <- st_as_sf(sights, coords = c("Longitude", "Latitude"),
crs = 4326) %>% st_transform(sights, crs = "+proj=utm +zone=48 +datum=WGS84 +units=m +no_defs")
p <- ggplot() + geom_sf(data = occupancies, aes(fill = Occ)) +
geom_sf(data = sights, aes(colour = Elevation)) + scale_colour_distiller(palette = "PRGn") +
scale_fill_distiller(palette = "YlOrRd")
Uses “frame” argument to index time
df <- data.frame(x = runif(1000), y = runif(1000), f = 1:1000) p <- ggplot(df, aes(x, y)) + geom_point(aes(frame = f)) p
No frame argument for ggplot but ggplotly knows what to do
ggplotly(p)
p <- matrics_long %>% ggplot(aes(x = year, y = pass_rate, colour = province)) + geom_line(aes(frame = year)) + geom_point(aes(frame = year)) + scale_colour_brewer(palette = "Set3") p
ggplotly(p)
pgap <- ggplot( gapminder, aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(aes(frame = year), show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy")
pgap
ggplotly(pgap)
gganimate packagepgap + transition_time(year) +
labs(title = "Year: {frame_time}")
Adds transitions - functions that determine how the plot data should be distributed over frames
transition_timetransition_statestransition_revealtransition_manualUses tweening - calculates intermediate data for a smooth transition between the states
Easing controls how these intermediate data are calculated (defaults to linear)
transition_statesTransition between several distinct stages of the data
states: column with state levels.transition_length: length of the transition between states. +state_length: length of the pause at the states.wrap = TRUE: if TRUE the last state will be transitioned into the first.transition_statespgap <- ggplot(
gapminder,
aes(x = gdpPercap, y=lifeExp, size = pop, colour = year)) +
geom_point(show.legend = FALSE, alpha = 0.7) +
scale_color_viridis_c() +
scale_size(range = c(2, 12)) +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy") +
transition_states(continent) +
labs(title = "Continent: {closest_state}")
transition_statesSomething not quite right
transition_statesNeed to add group = continent to aes()
pgap2 <- ggplot( gapminder, aes(x = gdpPercap, y=lifeExp, size = pop, colour = year, group = continent)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_c() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy")
transition_states